base

base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like set
, get
, del
and use
.
TOC
(TOC generated by verb using markdown-toc)
Install
Install with npm:
$ npm install base --save
Usage
var base = require('base');
inherit
function App() {
base.call(this);
}
base.extend(App);
var app = new App();
app.set('a', 'b');
app.get('a');
instantiate
var app = base();
app.set('foo', 'bar');
console.log(app.foo);
Inherit or instantiate with a namespace
A .namespace()
method is exposed on the exported function to allow you to create a custom namespace for setting/getting on the instance.
var Base = require('base')
var base = Base.namespace('cache');
var app = base();
app.set('foo', 'bar');
console.log(app.cache.foo);
API
All of the methods from cache-base are exposed on the base
API, as well as the following methods.
Create an instance of Base
with config
and options
.
Params
config
{Object}: passed to cache-baseoptions
{Object}
Example
var app = new Base({baz: 'qux'}, {yeah: 123, nope: 456});
app.set('foo', 'bar');
console.log(app.get('foo'));
console.log(app.get('baz'));
console.log(app.get('yeah'));
console.log(app.foo);
console.log(app.baz);
console.log(app.yeah);
console.log(app.options.yeah);
console.log(app.options.nope);
Set the given name
on app._name
and app.is*
properties. Used for doing lookups in plugins.
Params
name
{String}returns
{Boolean}
Example
app.is('foo');
console.log(app._name);
console.log(app.isFoo);
app.is('bar');
console.log(app.isFoo);
console.log(app.isBar);
console.log(app._name);
Returns true if a plugin has already been registered on an instance.
Plugin implementors are encouraged to use this first thing in a plugin
to prevent the plugin from being called more than once on the same
instance.
Params
name
{String}: The plugin name.register
{Boolean}: If the plugin if not already registered, to record it as being registered pass true
as the second argument.returns
{Boolean}: Returns true if a plugin is already registered.
Events
emits
: plugin
Emits the name of the plugin.
Example
var base = new Base();
base.use(function(app) {
if (app.isRegistered('myPlugin')) return;
});
base.use(function(app) {
if (app.isRegistered('myPlugin', true)) return;
});
Throws an error when plugin name
is not registered.
Params
name
{String}: The plugin name.
Example
var base = new Base();
base.use(function(app) {
app.assertPlugin('base-foo');
app.assertPlugin('base-bar');
app.assertPlugin('base-baz');
});
Define a plugin function to be called immediately upon init. Plugins are chainable and the only parameter exposed to the plugin is the application instance.
Params
fn
{Function}: plugin function to callreturns
{Object}: Returns the item instance for chaining.
Events
emits
: use
with no arguments.
Example
var app = new Base()
.use(foo)
.use(bar)
.use(baz)
Lazily invoke a registered plugin. Note that this method can only be used with:
- plugins that add a single method or property to
app
- plugins that do not (themselves) add a getter/setter property (they're already lazy)
- plugins that do not return a function
Params
prop
{String}: The name of the property or method added by the plugin.fn
{Function}: The plugin functionoptions
{Object}: Options to use when the plugin is invoked.returns
{Object}: Returns the instance for chaining
Example
app.lazy('store', require('base-store'));
Define a non-enumerable property on the instance. Dot-notation is not supported with define
.
Params
key
{String}: The name of the property to define.value
{any}returns
{Object}: Returns the instance for chaining.
Events
emits
: define
with key
and value
as arguments.
Example
define('render', function(str, locals) {
return _.template(str)(locals);
});
Mix property key
onto the Base prototype. If base-methods
is inherited using Base.extend
this method will be overridden
by a new mixin
method that will only add properties to the
prototype of the inheriting application.
Params
key
{String}val
{Object|Array}returns
{Object}: Returns the instance for chaining.
Static method for adding global plugin functions that will be added to an instance when created.
Params
fn
{Function}: Plugin function to use on each instance.
Example
Base.use(function(app) {
app.foo = 'bar';
});
var app = new Base();
console.log(app.foo);
Static method for inheriting both the prototype and
static methods of the Base
class. See class-utils
for more details.
Static method for adding mixins to the prototype. When a function is returned from the mixin plugin, it will be added to an array so it can be used on inheriting classes via Base.mixins(Child)
.
Params
fn
{Function}: Function to call
Example
Base.mixin(function fn(proto) {
proto.foo = function(msg) {
return 'foo ' + msg;
};
return fn;
});
Static method for running currently saved global mixin functions against a child constructor.
Params
Child
{Function}: Constructor function of a child class
Example
Base.extend(Child);
Base.mixins(Child);
Similar to util.inherit
, but copies all static properties,
prototype properties, and descriptors from Provider
to Receiver
.
class-utils for more details.
Test coverage
Statements : 100% ( 109/109 )
Branches : 100% ( 27/27 )
Functions : 100% ( 23/23 )
Lines : 100% ( 108/108 )
Related projects
There are a number of different plugins available for extending base. Let us know if you create your own!
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Building docs
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016 Jon Schlinkert
Released under the MIT license.
This file was generated by verb, v0.9.0, on March 09, 2016.